// -*- coding: utf-8 -*-
/*
 *   Copyright (C) 2013 by William Wong <librehat@outlook.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License as
 *   published by the Free Software Foundation; either version 2 or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import QtQuick 2.0
import QtQuick.XmlListModel 2.0

Item {
    id: youdaoAPI
    signal dataReady(string data)
    XmlListModel {
        id: ydModel;
        query: '/youdao-fanyi';
        XmlRole { name: 'errcode'; query: 'errorCode/string()' }
        XmlRole { name: 'word'; query: 'query/string()' }
        XmlRole { name: 'translation'; query: 'translation/string()' }//YOUDAO Translation
        XmlRole { name: 'phonetic'; query: 'basic/phonetic/string()' }//YOUDAO Basic Dictionary us-phonetic
        XmlRole { name: 'us_phonetic'; query: 'basic/us-phonetic/string()' }
        XmlRole { name: 'uk_phonetic'; query: 'basic/phonetic/string()' }
        XmlRole { name: 'explains'; query: 'basic/explains/string()' }
        XmlRole { name: 'webkey' ; query: 'web/explain[1]/key/string()' }//YOUDAO Web Dictionary
        XmlRole { name: 'web'; query: 'web/explain[1]/value/string()' }
        XmlRole { name: 'webkey2' ; query: 'web/explain[2]/key/string()' }
        XmlRole { name: 'web2'; query: 'web/explain[2]/value/string()' }
        XmlRole { name: 'webkey3' ; query: 'web/explain[3]/key/string()' }
        XmlRole { name: 'web3'; query: 'web/explain[3]/value/string()' }

        //onCountChanged: parseYD();
        onStatusChanged:{
            switch (status) {
                case XmlListModel.Null:
                    //console.log("null")
                    break
                case XmlListModel.Ready:
                    //console.log("ready")
                    parseYD()
                    break;
                case XmlListModel.Loading:
                    //console.log("loading...")
                    break;
                case XmlListModel.Error:
                    console.log("error:")
                    console.log(errorString())
                    break;
                default:
                    console.log("unkonwn error")
                    break;
            }
        }
    }

    function queryYD(words) {
        ydModel.reload()
        ydModel.source = 'http://fanyi.youdao.com/openapi.do?keyfrom=KDictionary&key=813148993&type=data&doctype=xml&version=1.1&q=' + words;
        
    }

    function parseYD() {
        var desresult = ""
        if (ydModel.get(0).errcode !== '0') {
            switch (ydModel.get(0).errcode) {
                case '20': desresult += qsTr('The input text is too long.'); break;
                case '30': desresult += qsTr('Cannot translate it correctly.'); break;
                case '40': desresult += qsTr('Unsupported language.'); break;
                case '50': desresult += qsTr('Invalid key.'); break;
                default: desresult += qsTr('Unknown error.');
            }
            return;//stop it
        }
        
        if (ydModel.get(0).word !== '')
            desresult += '<b style="color:#FF0000;font-size:20px;">' + ydModel.get(0).word + '</b><br />';
        
        if (ydModel.get(0).uk_phonetic !== '')
            desresult += '<b>' + qsTr('英:') + '&nbsp;</b>[' + ydModel.get(0).uk_phonetic + ']';
        if (ydModel.get(0).us_phonetic !== '')
            desresult += '<b>&nbsp;' + qsTr('美:') + '&nbsp;</b>[' + ydModel.get(0).us_phonetic + ']';
        desresult += '<br />'
        
        if (ydModel.get(0).explains !== '')
            desresult += ydModel.get(0).explains.replace(/\n/g, '<br/>') + ' <br/>';
        
        if (ydModel.get(0).translation !== '')
            desresult += '<b>' + qsTr('翻译:') + '&nbsp;&nbsp;</b><br />'  + ydModel.get(0).translation + '<br /><br />';
        if (ydModel.get(0).webkey !== '')
            desresult += '<b>' + qsTr('网络释义: ') + '</b><br />' + ydModel.get(0).webkey + qsTr(': ') + ydModel.get(0).web + '<br />' + ydModel.get(0).webkey2 + qsTr(': ') + ydModel.get(0).web2 + '<br />' + ydModel.get(0).webkey3 + qsTr(': ') + ydModel.get(0).web3;
        desresult += '<br/><br/><a href="http://dict.youdao.com/w/'+ydModel.get(0).word+'/#keyfrom=dict2.top">网页中查看</a>'
        emit: dataReady(desresult)
    }
}
